from html.parser import HTMLParser
import sys
import re
import pokeparser

class pokeinfo(HTMLParser):
	p = pokeparser.pokeparser()

	def __init__(self, url):
		super().__init__()
		self.printed = self.in_form = self.in_type = self.hiddenability = self.in_high = self.in_weigh \
			= self.in_gender = self.out_form = self.hp = self.at = self.df = self.sa = self.sd = self.sp = False
		self.display = True
		self.types = self.lasttype = self.abilitys = self.high = self.weigh = self.species = ''
		self.abilitycount = 0
		self.gender = '-1'
		self.info = []
		self.text = self.p.gettext(url)

	def handle_starttag(self, tag, attrs):
		attrs = dict(attrs)
		title = attrs.get('title', '')
		# 不同形态宝可梦只显示一种形态的信息：
		display = attrs.get('style', '')
		cl = attrs.get('class', '')
		if re.search('_toggle form(.+?)', cl):
			if display == 'display:none;':
				self.display = False
			else:
				self.display = True

		if self.display:
			# 获得宝可梦属性：
			match = re.search('(.+?)（属性）', title)
			if match and self.out_form == False:
				ty = match.group(1)
				self.in_form = True
				if ty != self.lasttype:
					if self.in_type:
						self.types += '/'
					self.types += ty
					self.lasttype = ty
				self.in_type = True

			elif self.in_form:
				# 获得宝可梦特性：
				if re.search('(.+?)（特性）', title):
					self.in_type = False
					if self.abilitycount > 0:
						self.abilitys += '/'
					self.abilitys += (re.search('(.+?)（特性）', title).group(1))
					self.abilitycount += 1
				elif tag == 'small':
					self.hiddenability = True
				# 获得种族值前，组合全部信息：
				elif re.search('基础点数', title):
					self.info.append(self.types)
					self.info.append(self.abilitys)
					self.info.append(self.high)
					self.info.append(self.weigh)
					self.info.append(self.gender)
					self.abilitys = self.types = self.high = self.weigh = self.gender = ''
					self.in_gender = self.in_form = False
					self.out_form = True

		# 获得种族值：
		if self.out_form and self.printed == False:
			cl = attrs.get('class', '')
			st = attrs.get('style', 'void')
			match = re.search(r'width: calc\(100% \* (.+?)/255\); height:20px', st)

			if tag == 'div':
				if re.search('bd-HP bw-1 bg-HP', cl):
					self.hp = True
				elif re.search('bd-攻击 bw-1 bg-攻击', cl):
					self.at = True
				elif re.search('bd-防御 bw-1 bg-防御', cl):
					self.df = True
				elif re.search('bd-特攻 bw-1 bg-特攻', cl):
					self.sa = True
				elif re.search('bd-特防 bw-1 bg-特防', cl):
					self.sd = True
				elif re.search('bd-速度 bw-1 bg-速度', cl):
					self.sp = True
				if match:
					self.setSpecies(match.group(1))

	def handle_data(self, data):
		if self.in_form:
			# 划分特性、身高、体重、性别比：
			if data == '隐藏特性' and self.hiddenability:
				self.hiddenability = False
				if self.abilitycount == 2:
					self.abilitys = self.abilitys.replace('/', '//')
				self.abilitycount = 0
			elif data == '身高':
				self.in_high = True
			elif data == '体重':
				self.in_weigh = True
			elif data == '性别比例':
				self.in_gender = True
			# 获得身高：
			if self.in_high:
				m = re.search('(.+?m)', data)
				if m:
					self.high = m.group(1)
					self.in_high = False
			# 获得体重：
			elif self.in_weigh:
				m = re.search('(.+?kg)', data)
				if m:
					self.weigh = m.group(1)
					self.in_weigh = False
			# 获得性别比：
			elif self.in_gender:
				self.setGender(data)

	# 设置性别比：
	def setGender(self, data):
		m = re.search('(.+?)性 (.+?%)', data)
		if m:
			if m.group(1) == '雄':
				self.gender = m.group(2)
			elif m.group(1) == '雌' and m.group(2) == '100%':
				self.gender = '0%'
			elif m.group(1) == '雌':
				pass
			self.in_gender = False

	# 设置种族值，最后打印全部信息：
	def setSpecies(self, wi):
		if self.hp:
			self.species += wi
			self.hp = False
		elif self.at:
			self.species += '/'
			self.species += wi
			self.at = False
		elif self.df:
			self.species += '/'
			self.species += wi
			self.df = False
		elif self.sa:
			self.species += '/'
			self.species += wi
			self.sa = False
		elif self.sd:
			self.species += '/'
			self.species += wi
			self.sd = False
		elif self.sp:
			self.species += '/'
			self.species += wi
			self.sp = False
			self.info.append(self.species)
			self.species = ''
			self.printed = True

def test():
	p = pokeinfo(r'https://wiki.52poke.com/zh-hans/%E5%A6%99%E8%9B%99%E7%A7%8D%E5%AD%90')
	p.feed(p.text)
	print(p.info)
	p = pokeinfo(r'https://wiki.52poke.com/zh-hans/%E6%B4%9B%E6%89%98%E5%A7%86')
	p.feed(p.text)
	print(p.info)
	p = pokeinfo(r'https://wiki.52poke.com/zh-hans/%E5%93%88%E5%8A%9B%E6%A0%97')
	p.feed(p.text)
	print(p.info)
	p = pokeinfo(r'https://wiki.52poke.com/zh-hans/%E5%9F%BA%E6%A0%BC%E5%B0%94%E5%BE%B7')
	p.feed(p.text)
	print(p.info)
	p = pokeinfo(r'https://wiki.52poke.com/zh-hans/%E5%8D%A1%E7%92%9E%E3%83%BB%E9%B8%A3%E9%B8%A3')
	p.feed(p.text)
	print(p.info)
	p = pokeinfo(r'https://wiki.52poke.com/zh-hans/%E6%AF%92%E8%B4%9D%E6%AF%94')
	p.feed(p.text)
	print(p.info)
	p.close()

if __name__ == "__main__":
	test()